#include "Red.hpp"

using namespace red;


int main(int argc, char** argv){
	Device device;
	device.Initialize(800, 600, "Red Project");

	device.GetLua().LoadScript("main.lua");
	
	ui::Label t, tl;

	t.SetPosition(Vector2F(300,300));
	t.UseBorder(true, 1);
	t.SetBackgroundColor(Color(0,0,0,0.7f));
	t.SetTextSize(12);

	tl.SetAlignment(ui::TopLeft);
	tl.SetText("TopLeft");

	device.GetGui().Add(t);
	device.GetGui().Add(tl);
	


	sf::Image mImage;
	sf::Sprite mSprite;
	mImage.LoadFromFile("RedSoftworks.png");
	mSprite.SetImage(mImage);

	mSprite.Resize(sf::Vector2f(800,300));
	mSprite.SetColor(sf::Color(255,255,255,50));

	sf::Vector2f sPos(0,150);
	Clock clk, fpsClock;
	f32 time;

	while(device.IsActive()){
		time = clk.GetElapsedTime();
		clk.Reset();

		if(fpsClock.GetElapsedTime() > 0.5f){
			t.SetText(String("FPS : ")+(1.f/time));
			fpsClock.Reset();
		}

		// Events. Not Smooth
		while (device.GetInput().GetEvents()){
			if(device.GetInput().IsKeyHit(red::Escape))
				device.Shutdown();

			device.GetGui().SendEvent(device.GetInput().GetLastEvent());
		}
		// Events End.

		// Inputs. Smooth
		if(device.GetInput().IsKeyDown(D))
			sPos.x += 100 * time;
		if(device.GetInput().IsKeyDown(Q))
			sPos.x -= 100 * time;
		if(device.GetInput().IsKeyDown(Z))
			sPos.y -= 100 * time;
		if(device.GetInput().IsKeyDown(S))
			sPos.y += 100 * time;
		// Inputs End.

		mSprite.SetPosition(sPos);

		device.BeginRender();
			device.GetWindow().Draw(mSprite);
			device.GetGui().Display();
		device.EndRender();
	}
}
